<?php
/**
 * Application level View Helper
 *
 * This file is application-wide helper file. You can put all
 * application-wide helper-related methods here.
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.View.Helper
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */
App::uses('Helper', 'View');
App::import("Model", "Advertise");

/**
 * Application helper
 *
 * Add your application-wide methods in the class below, your helpers
 * will inherit them.
 *
 * @package       app.View.Helper
 */
class AppHelper extends Helper {
	/*
     * @description: mixLayout
     * @param: string $content
     * @param: string $image
     * @param: string $layout (html)
     * @since : 25/12/2013
     * @author: nguyennb6390@setacinq.com.vn
     * @return string html
     */

    public function mixLayout($content, $image, $layout) {
        $s3Path = Configure::read('amazonS3.bucketUrl');
        if(strlen($s3Path) + 3 > strlen($image)){
            $image = '';
        }else{

        }
        $result = str_replace("content_fill", h($content), $layout);
        $result = str_replace("image_fill", $image, $result);

        return nl2br($result);
    }

    /*
     * @description: getAdvertises
     * @param: int $officeId
     * @param: string $type
     * @since : 25/12/2013
     * @author: nguyennb6390@setacinq.com.vn
     * @return string html
     */

    public function getAdvertises($type = 'HD') {
        $model = new Advertise();
        $result = $model->getAdvertiseView($type);
        if($result == null){
            return '';
        }
        return '<div class="ad_area">' .  html_entity_decode($result) . '</div>';

    }
    /*
     * @description: isActice
     * @param: string $menuNameActive
     * @since : 27/12/2013
     * @author: Nguyen Binh Nguyen <nguyennb6390@setacinq.com.vn>
     * @return string 'active' or ''
     */

    public function isActice($menuNameActive = Null) {
        $controllerName = $this->params['controller'];
        $actionName = $this->params['action'];

        $arrControllerAction = array(
            'articles' => array(
                'article',
                'detail',
            ),
            'bookings' => array(
                'index',
                'history'
            ),
            'faq' => array(
                'index',
            ),
            'payments' => array(
                'index',
            ),
            'contracts' => array(
                'withdrawals',
                'index',
                'changeOp'
            ),
            'freeadrsAreas' => array(
                'index',
            ),
            'transfers' => array(
                'index'
            )
        );

        if (array_key_exists($controllerName, $arrControllerAction) && $menuNameActive == $controllerName) {
            if (in_array($actionName, $arrControllerAction[$controllerName])) {
                return 'active';
            }
        }
        return '';
    }


	# Admin
	/*
     * @description: textStatusGroup
     * @param: int $status
     * @since : 06/02/2014
     * @author: nguyennb6390@setacinq.com.vn
     * @return string
     */

    public function textStatusGroup($status) {
        $text = '';

        if ($status == ConstAdminComponent::OFFICE_STATUS_PUBLIC) {
            $text = __('public group');
        } else {
            $text = __('private group');
        }
        return $text;
    }

	# Admin
	/*
     * @description: textUseMutualGroup
     * @param: int $useMutual
     * @since : 26/03/2014
     * @author: nguyennb6390@setacinq.com.vn
     * @return string
     */

    public function textUseMutualGroup($useMutual) {
        $text = '';

        if ($useMutual == ConstAdminComponent::OFFICE_USE_MUTUAL_ENABLE) {
            $text = __('use mutual enable');
        } else {
            $text = __('use mutual disable');
        }
        return $text;
    }

    /*
     * @package secretKey
     * @category secretKey
     * @author MiengTQ
     * @author Miengtq6421@setacinq.com.vn
     * @author MiengTq
     * @copyright 2010 Knowledge Society
     * @version 1.0.0
     * @link http://www.k-society.com
     */

    public function secretKey($id) {
        return $id;
        //$office_id =
        $userId = AuthComponent::user('id');
        $officeId = AuthComponent::user('office_id');
        $key = $officeId . 'ABCDEF123' . $userId . 'ABCDEF123' . $id;
        $leng = strlen($key);
        if($leng < 10){
            $leng = '000' . $leng;
        }elseif($leng < 100){
            $leng = '00' . $leng;
        }elseif($leng < 1000){
            $leng = '0' . $leng;
        }

        $key .= 'ABCDEF123' . $leng;

        $keyView = base64_encode( $key);

        return $keyView;
    }

        /*
     * @package getTransferCycle
     * @category getTransferCycle
     * @author MiengTQ
     * @author Miengtq6421@setacinq.com.vn
     * @author MiengTq
     * @copyright 2010 Knowledge Society
     * @version 1.0.0
     * @link http://www.k-society.com
     */
    public function getTransferCycle($transferCycle) {
        if ($transferCycle == 'RT') {
            return 'リアルタイム';
        } elseif ($transferCycle == 'W1') {
            return '1週間に1回';
        } elseif ($transferCycle == 'M1') {
            return '1ヶ月に1回';
        } else {
            return '';
        }
    }
    /*
     * @package Post:
     * @category isPostOfficeBox
     * @author MiengTQ
     * @author Miengtq6421@setacinq.com.vn
     * @author MiengTq
     * @copyright 2010 Knowledge Society
     * @version 1.0.0
     * @link http://www.k-society.com
     */
    public function isPostOfficeBox($isPostOfficeBox) {
        if ($isPostOfficeBox == 0) {
            return '無し';
        } elseif ($isPostOfficeBox == 1) {
            return '有り';
        } else {
            return '';
        }
    }
    /*
     * @package Post:
     * @category standardType
     * @author MiengTQ
     * @author Miengtq6421@setacinq.com.vn
     * @author MiengTq
     * @copyright 2010 Knowledge Society
     * @version 1.0.0
     * @link http://www.k-society.com
     */
    public function standardType($standardType) {
        if ($standardType == 0) {
            return '規格外';
        } elseif ($standardType == 1) {
            return '規格内';
        } else {
            return '';
        }
    }
    /*
     * @package Post
     * @category deliveryCompany
     * @author MiengTQ
     * @author Miengtq6421@setacinq.com.vn
     * @author MiengTq
     * @copyright 2010 Knowledge Society
     * @version 1.0.0
     * @link http://www.k-society.com
     */
    public function deliveryCompany($deliveryCompany) {
        switch ($deliveryCompany) {
            case 10:
                return 'クロネコヤマト';
            case 20:
                return '佐川急便';
            case 30:
                return 'FedEx';
            case 40:
                return 'バイク便';
            case 90:
                return '指定しない';
            default:
                //return '指定しない';
                return '';
        }
    }
    /*
     * @package Post:
     * @category forwardType
     * @author MiengTQ
     * @author Miengtq6421@setacinq.com.vn
     * @author MiengTq
     * @copyright 2010 Knowledge Society
     * @version 1.0.0
     * @link http://www.k-society.com
     */
    public function forwardType($forwardType) {
        switch ($forwardType) {
            case 10:
                return 'メール便';
            case 20:
                return '宅配便';
            case 30:
                return '不在票';
            default:
                //return '指定しない';
                return '';
        }
    }
    
    /*
     * @package Post:
     * @category forwardType
     * @author namnd
     * @author namnd6432@setacinq.com.vn
     * @author MiengTq
     * @copyright 2010 Knowledge Society
     * @version 1.0.0
     * @link http://www.k-society.com
     */
    public function forwardTypeReqprofile($forwardType) {
        switch ($forwardType) {
            case 1:
                return '本社に転送';
            case 2:
                return ' 契約者に転送';
            case 3:
                return ' 正会員、またはサブ会員に転送';
            default:
                return '指定しない';
                //return '';
        }
    }
    
    
    /*
     * @package Post
     * @category getPostStatus
     * @author MiengTQ
     * @author Miengtq6421@setacinq.com.vn
     * @author MiengTq
     * @copyright 2010 Knowledge Society
     * @version 1.0.0
     * @link http://www.k-society.com
     */
    public function getPostStatus($status) {
        switch ($status) {
            case 10:
                return '受領';
            case 20:
                return '発送';
            case 21:
                return '私書箱';
            case 22:
                return '窓口引渡';
            case 30:
                return 'キャンセル';

            default:
                //return '指定しない';
                return '';
        }
    }
    /*
     * @author namnd
     * @author namnd6432@setacinq.com.vn
     * @author namnd
     * @copyright 2010 Knowledge Society
     * @version 1.0.0
     * @link http://www.k-society.com
     */
    public function getBizform($bizform) {
        switch ($bizform) {
            case 1:
                return '法人';
            case 2:
                return '個人';
            default:
                return '';
        }
    }
    /*
     * @author namnd
     * @author namnd6432@setacinq.com.vn
     * @author namnd
     * @copyright 2010 Knowledge Society
     * @version 1.0.0
     * @link http://www.k-society.com
     */
    public function getPayee($payee) {
        switch ($payee) {
            case 1:
                return 'リコー';
            case 2:
                return 'りそな';
            case 3:
                return 'PayPal';
            case 9:
                return 'その他';
            default:
                return 'その他';
        }
    }
    /*
     * @author namnd
     * @author namnd6432@setacinq.com.vn
     * @author namnd
     * @copyright 2010 Knowledge Society
     * @version 1.0.0
     * @link http://www.k-society.com
     */
    public function getexaminationOutcome($payee) {
        switch ($payee) {
            case '0':
                return '保留';
            case '10':
                return '承認';
            case '20':
                return '却下';
            default:
                return '';
        }
    }
    /*
     * @author namnd
     * @author namnd6432@setacinq.com.vn
     * @author namnd
     * @copyright 2010 Knowledge Society
     * @version 1.0.0
     * @link http://www.k-society.com
     */
     public function getfinancialDepositType($payee) {
        switch ($payee) {
            case 1:
                return '普通預金';
            case 2:
                return '当座預金';
            case 3:
                return '貯蓄預金';
            case 4:
                return '定期預金';
            case 5:
                return '積立預金・積立定期預金';
            case 6:
                return '定期積金';
            case 7:
                return '通知預金';
            case 8:
                return '納税準備預金';
            case 9:
                return '別段預金';
            default:
                return '指定しない';
        }
    }
    /*
     * @author namnd
     * @author namnd6432@setacinq.com.vn
     * @author namnd
     * @copyright 2010 Knowledge Society
     * @version 1.0.0
     * @link http://www.k-society.com
     */
     public function getPaymentMethod($value) {
        switch ($value) {
            case 1:
                return '口座振替';
            case 2:
                return 'クレジット払い';
            case 3:
                return '現金払い';
            case 4:
                return '口座振込';
            case 9:
                return 'その他';
            default:
                return 'その他';
        }
    }
    /*
     * @author namnd
     * @author namnd6432@setacinq.com.vn
     * @author namnd
     * @copyright 2010 Knowledge Society
     * @version 1.0.0
     * @link http://www.k-society.com
     */
     public function getExaminationRank($value) {
        switch ($value) {
            case 1:
                return 'Rank1';
            case 2:
                return 'Rank2';
            case 3:
                return 'Rank3';
            case 4:
                return 'Rank4';
            case 5:
                return 'Rank5';
             default:
                return '指定しない';
        }

    }
    /*
     * @author namnd
     * @author namnd6432@setacinq.com.vn
     * @author namnd
     * @copyright 2010 Knowledge Society
     * @version 1.0.0
     * @link http://www.k-society.com
     */
    public function getisPostalForward($value) {
        switch ($value) {
            case 0:
                return '転送先に指定しない';
            case 1:
                return '転送先に指定する';
            default:
                return '指定しない';
        }
    }
    /*
     * @author namnd
     * @author namnd6432@setacinq.com.vn
     * @author namnd
     * @copyright 2010 Knowledge Society
     * @version 1.0.0
     * @link http://www.k-society.com
     */
    public function getWithdrawalReasonType($value) {
        switch ($value) {
            case 1:
                return '料金に不満';
            case 2:
                return 'サービスに不満';
            case 3:
                return '業務拡大のため';
            case 4:
                return '廃業・休業のため';
            case 5:
                return '他社サービスを利用';
            case 9:
                return 'その他(具体的にお書きください)';
            default:
                return 'その他';
        }

    }    
    /*
     * @author namnd
     * @author namnd6432@setacinq.com.vn
     * @author namnd
     * @copyright 2010 Knowledge Society
     * @version 1.0.0
     * @link http://www.k-society.com
     */
    public function getFilingFlg($value) {
        switch ($value) {
            case '0':
                return '住所で登記していない';
            case '1':
                return '住所で登記している';
            default:
                return 'その他';
        }
    }
    /*
     * @author namnd
     * @author namnd6432@setacinq.com.vn
     * @author namnd
     * @copyright 2010 Knowledge Society
     * @version 1.0.0
     * @link http://www.k-society.com
     */
    public function getDepositPayeeFlg($value) {
        switch ($value) {
            case '0':
                return '登録済み金融機関に振り込み';
            case '1':
                return '指定金融機関に振込み';
            default:
                return 'その他';
        }
    }
     /*
     * @author namnd
     * @author namnd6432@setacinq.com.vn
     * @author namnd
     * @copyright 2010 Knowledge Society
     * @version 1.0.0
     * @link http://www.k-society.com
     */
    public function getSeq($value) {
        switch ($value) {
            case 0:
                return '正会員';
            case 1:
                return 'サブ会員';
            default:
                return 'サブ会員';
        }
    }
    /*
     * @author namnd
     * @author namnd6432@setacinq.com.vn
     * @author namnd
     * @copyright 2010 Knowledge Society
     * @version 1.0.0
     * @link http://www.k-society.com
     */
    public function getUpdateType($value) {
        switch ($value) {
            case 1:
                return '追加申請';
            case 2:
                return '更新申請';
            case 3:
                return '削除申請';
        }
    }

    public  function getUrl($url=null) {
        $page = $this->request->param('named');
        $page = (isset($page['page'])) ? $page['page'] : 1;
        $params ='';
        if ($this->request->query) {
            $i=0;
            foreach ($this->request->query as $key => $param) {
                if ($i== 0) {
                    $params .= '?'.$key.'='.urlencode($param);
                } else {
                    $params .= '&'.$key.'='.urlencode($param);
                }
                $i++;
            }
        } else {
            $params = '?tmp=true';
        }
        if (!$this->request->query('page')) {
            $params .= '&page='.$page;
        }
        return $params;
    }

    public  function getUrlNoPage() {
        $page = $this->request->param('named');
        $page = (isset($page['page'])) ? $page['page'] : 1;
        $params ='';
        if ($this->request->query) {
            $i=0;
            foreach ($this->request->query as $key => $param) {
                if ($i== 0) {
                    $params .= '?'.$key.'='.urlencode($param);
                } else {
                    if ($key !=='page') {
                        $params .= '&'.$key.'='.urlencode($param);
                    }
                }
                $i++;

            }
        }
        return $params;
    }

    public  function getExportUrl() {
        $exportUrl = $this->request->url;
        $exportUrl .= '?export=true';
        if ($this->request->query) {
            foreach ($this->request->query as $key => $param) {
                $exportUrl .= '&'.$key.'='.urlencode($param);
            }
        }
        return $exportUrl;
    }

    public function convertBreakLineToHtml($string) {
        return str_replace("\n", "<br>", $string);
    }

}
